﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
//using System.Xml.Linq;
using System.Collections.Generic;

using YouHP.BLL;
using YouHP.Model;
using YouHP.Model.Group;
using YouHP.Utility;

namespace web.admin
{
    public partial class GroupNewsEdit : System.Web.UI.Page
    {
        private string URL_BACK = "GroupNews.aspx?groupId=";
        private GroupNewsBLL groupNewsBll = GroupNewsBLL.Instance;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Bind();
            }
        }

        private void Bind()
        {
            try
            {
                int id = RequestTools.GetQueryInt("id", -1);
                if (id > 0)
                {
                    IList list = new ArrayList();
                    GroupNewsInfo info = groupNewsBll.GetGroupNewsInfo(id);
                    if (info != null)
                    {
                        if (info.LinkUrl != null && info.LinkUrl.Length > 0)
                        {
                            this.ViewState["isUrl"] = "1";
                        }
                        else
                        {
                            this.ViewState["isUrl"] = "0";
                        }
                        list.Add(info);
                    }
                    this.ViewState["groupId"] = "" + info.GroupId;

                    this.groupNewsInfo.DataSource = list;
                    this.groupNewsInfo.DataBind();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        protected void DataBound(object sender, EventArgs e)
        {
            try
            {
                ((RadioButtonList)this.groupNewsInfo.FindControl("isUrl")).SelectedValue = this.ViewState["isUrl"].ToString();
                if (this.ViewState["isUrl"].ToString().Equals("1"))
                {
                    this.groupNewsInfo.Fields[3].Visible = true;
                    this.groupNewsInfo.Fields[4].Visible = false;
                    this.groupNewsInfo.Fields[5].Visible = false;
                }
                else
                {
                    this.groupNewsInfo.Fields[3].Visible = false;
                    this.groupNewsInfo.Fields[4].Visible = true;
                    this.groupNewsInfo.Fields[5].Visible = true;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        protected void UpdateButton_click(object sender, EventArgs e)
        {
            try
            {
                GroupNewsInfo info = validateForm(this.groupNewsInfo);

                if (info != null)
                {
                    info.Id = Convert.ToInt32(this.groupNewsInfo.DataKey.Value);
                    if (groupNewsBll.UpdateGroupNews(info))
                    {
                        HttpContext.Current.Response.Redirect(URL_BACK + this.ViewState["groupId"].ToString(), true);
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        protected void IsUrlChanged(object sender, EventArgs e)
        {
            if (((RadioButtonList)this.groupNewsInfo.FindControl("isUrl")).SelectedValue.Equals("1"))
            {
                this.groupNewsInfo.Fields[3].Visible = true;
                this.groupNewsInfo.Fields[4].Visible = false;
                this.groupNewsInfo.Fields[5].Visible = false;
            }
            else
            {
                this.groupNewsInfo.Fields[3].Visible = false;
                this.groupNewsInfo.Fields[4].Visible = true;
                this.groupNewsInfo.Fields[5].Visible = true;
            }

            this.ViewState["isUrl"] = ((RadioButtonList)this.groupNewsInfo.FindControl("isUrl")).SelectedValue;
        }

        protected void CancelButton_click(object sender, EventArgs e)
        {
            HttpContext.Current.Response.Redirect(URL_BACK + this.ViewState["groupId"].ToString(), true);
        }

        private GroupNewsInfo validateForm(DetailsView formViewCtl)
        {
            GroupNewsInfo info = new GroupNewsInfo();

            info.GroupId = Convert.ToInt32(this.ViewState["groupId"].ToString());
            info.Title = ((TextBox)formViewCtl.FindControl("title")).Text;
            info.Introduction = ((TextBox)formViewCtl.FindControl("introduction")).Text;
            info.UpdateTime = DateTime.Now;

            if (this.ViewState["isUrl"].ToString().Equals("1"))
            {
                info.LinkUrl = ((TextBox)formViewCtl.FindControl("linkUrl")).Text;
            }
            else
            {
                info.Source = ((TextBox)formViewCtl.FindControl("source")).Text;
                info.Body = ((TextBox)formViewCtl.FindControl("body")).Text;
            }

            return info;
        }
    }
}

